package _01_动态数组;

import java.util.Arrays;

public class _492_构造矩形 {

    public static void main(String[] args) {

        _492_构造矩形 v = new _492_构造矩形();

        System.out.println(Arrays.toString(v.constructRectangle(7)));

    }

    // 一次遍历
    public int[] constructRectangle(int area) {
        int cellHalf = (int) Math.sqrt(area);
        // 找到第一个即值
        for (int i = cellHalf; i >= 1; i++) {
            if (area % i == 0) {
                return new int[]{area / i , i};
            }
        }
        return null;
    }

    // 遍历寻找
    public int[] constructRectangle1(int area) {
        int resWidth = 0;
        int resHeight = 0;
        int min = Integer.MAX_VALUE;
        for (int width = 1; width <= area; width++) {
            int height = area / width;
            if (width * height != area || width < height) continue;

            if (width - height < min) {
                resWidth = width;
                resHeight = height;
                min = width - height;
            }
        }
        return new int[]{resWidth, resHeight};
    }

}
